<template>
  <div class="outside">
    <div class="app-container" style="width: 92%">
      <inline-form
        ref="InlineForm"
        :item="item"
        @doSubmit="handleDoSubmit"
      ></inline-form>
    </div>
  </div>
</template>

<script>
import _form from "./_form";
import { queryItem, edit } from "./api/index";
import { addImage } from "../../../../utils/common";
export default {
  data() {
    return {
      id: null,
      item: {
        cardName: "",
        cardDesc: "",
        cardTime: "",
        price: "",
        jifen: "",
        image: "",
        imageFile: "",
      },
      updateItemFunc: (target) => {
        this.item = {
          cardName: target.cardName,
          cardDesc: target.cardDesc,
          cardTime: target.cardTime,
          price: target.price,
          jifen: target.jifen,
          image: target.image,
          imageFile: "img_none",
        };
      },
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.get_item();
  },
  methods: {
    get_item() {
      if (this.id) {
        // 查
        queryItem(this.id).then((data) => {
          console.log(data);
          if (data.code === 1) {
            let act = data.data;
            this.updateItemFunc(act);
            console.log(this.item);
          } else {
            this.$message.error(data.msg);
          }
        });
      } else {
        $this.$message.error("数据ID不存在，请检查后重写");
      }
    },
    // 修改
    async handleDoSubmit() {
      let form = this.$refs.InlineForm;
      form.valid(async (valid) => {
        if (valid) {
          let item = form.obj;
          console.log(item, "修改");
          if (
            item.imageFile &&
            item.imageFile !== "" &&
            item.imageFile !== "img_none"
          ) {
            item.image = await addImage(item.imageFile);
          }
          delete item.imageFile;
          const res = await edit({ id: this.id, ...item });
          console.log(res, "====>");
          if (res.code === 1) {
            this.$message.success("修改成功!");
            this.$router.go(-1);
          } else {
            this.$message.error(res.msg);
          }
        } else {
          this.$message.error("数据填写错误，请按要求填写!");
        }
      });
    },
  },
  components: {
    "inline-form": _form,
  },
};
</script>

<style scoped>
</style>
